const shop = require('../../providers/dataAccess').shop;

const FeatureService = function () {
    const _featureList = async() => {
      var szSql = "SELECT * FROM features WHERE status = 1";
      var features = await shop.query(szSql);
      if (features && features.length) {
        for (var i = 0; i < features.length; ++i) {
          var feature = features[i];
          //console.log(feature.id);
          szSql = "SELECT id,featureValue FROM feature_values WHERE featureId = ?";
          var values = await shop.query(szSql,[feature.id]);
          feature.featureValues = values;
        }
        return features;
      } else {
        return null;
      }
    };

    const _productFeatures = async (productId) => {
      var szSql = "SELECT featureId,GROUP_CONCAT(featureValueId SEPARATOR ',') as featureValues FROM mbs_shop.product_sku_features WHERE productId = ? group by featureId";
      var features = await shop.query(szSql,[productId]);
      if (features && features.length) {
         var allFeatures = await _featureList();
         for (var i = 0; i < features.length; ++i) {
           var feature = features[i];
           console.log("feature:" + JSON.stringify(feature));
           for (var j = 0; j < allFeatures.length; ++j) {
             var item = allFeatures[j];
             console.log("item:" + JSON.stringify(item));
             if (feature.featureId == item.id) {
               feature.featureName = item.featureName;
               var featureValueIds = feature.featureValues.toString().split(",");
               console.log(featureValueIds);
               var values = [];
               for (var value in item.featureValues) {
                 let id = item.featureValues[value].id.toString();
                 console.log("value:" + id + " indexOf:" + featureValueIds.indexOf(id));
                 if (featureValueIds.indexOf(id) > -1) {
                   values.push(item.featureValues[value]);
                 }
               }
               feature.featureValues = values;
             }
           }
         }
      }
      return features;
    };

    const _productMutilFeatures = async (productId) => {
      var szSql = "SELECT featureId,GROUP_CONCAT(distinct featureValueId SEPARATOR ',') as featureValues FROM mbs_shop.product_sku_features WHERE productId = ? group by featureId";
      var features = await shop.query(szSql,[productId]);
      let values = [];
      if (features && features.length) {
         var allFeatures = await _featureList();
         for (var i = 0; i < features.length; ++i) {
           var feature = features[i];
           var featureItems = [];
           for (var j = 0; j < allFeatures.length; ++j) {
             var item = allFeatures[j];
             //console.log(item);
             if (feature.featureId == item.id) {
               var featureValueIds = feature.featureValues.toString().split(",");
               for (var value in item.featureValues) {
                 let id = item.featureValues[value].id.toString();
                 if (featureValueIds.indexOf(id) > -1) {
                  // console.log(value);
                   //values.push(item.featureValues[value]);
                   featureItems.push(item.featureValues[value]);
                 }
               }
               //feature.featureValues = values;
             }
           }
           values.push(featureItems);
         }
      }
      return values;
    }

    return {
        list: _featureList,
        featureValues: _productFeatures,
        featureItems: _productMutilFeatures
    }
}();

module.exports = FeatureService;
